package com.xafc.demo.security.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xafc.demo.framework.result.ApiResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: ZhangMing
 * @Date: 2021/4/13 15:09
 */
public class ApiResultUtil {

    public static void responseJson(HttpServletResponse response, ApiResult apiResult){
        // 返回Json数据
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "text/html;charset=UTF-8");  //这句话是解决乱码的
        PrintWriter out = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            String result = mapper.writeValueAsString(apiResult);

            out = response.getWriter();
            out.print(result);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

}
